<?php
	//充值卡支付接口-cardpay.php
	//create by L
	//http://42.121.57.193:7788/platform/cardpay.php?phone=13222422341&card_no=0000000&card_pwd=111111&card_type=1&money=30

	//card_type 1为联通 0为移动
	//初始化--
	include_once "init.php";

	$phone        	= $_GET["phone"];
	$card_no        = $_GET["card_no"];
	$card_pwd       = $_GET["card_pwd"];
	$card_type      = $_GET["card_type"];
	$money        	= $_GET["money"];

	if('' == $phone )
	{
		die("-1|手机号码不可以为空");
	}
	if ('' == $card_no)
	{
		die("-1|充值卡号码不能为空");
	}
	if ('' == $card_pwd)
	{
		die("-1|充值卡密码不能为空");
	}
		if ('' == $card_type)
	{
		die("-1|充值卡类型不能为空");
	}
	if ('' == $money)
	{
		die("-1|充值金额不能为空");
	}
	if (false==is_numeric($card_no))
	{
		die("-1|充值卡号必须为数字");
	}
	if (false==is_numeric($card_pwd))
	{
		die("-1|充值密码必须为数字");
	}
	if (strlen($phone) != 11 || false==is_numeric($phone) || ord($phone)!=49)
	{
		die("-1|手机号码必须是11位数字，并且以1开头");
	}

	$flag = 0;
	$time = date('Y-m-d H:i:s');
	$con = db_connect(DB_NAME);

	//查询USER表看号码是否是注册用户
	$result = mysql_query("SELECT * from user where phone='$phone'") or die("-1|查询user表失败");
	$row 	= mysql_fetch_row($result);

	if (empty($row))
	{
		mysql_close($con);
		die("-1|该手机号码未注册");
	}
	else
	{
		$uuid_md5 = md5($row['0']);
		/*
		//判断充值记录表中卡号和输入卡号是否相同，相同不进行充值处理，避免重复充值
		$result = mysql_query("SELECT * from recharge where card_no='$card_no'") or die("-1|查询user表失败");
		$row_recharge 	= mysql_fetch_row($result);
		if (!empty($row_recharge))
		{
			die("-1|该充值卡号已经被使用");
		}
		*/
		//调用第三方充值接口-成功的话写入charge表
		$data = '';
		$data = cardpay(
					$uuid_md5,			//充值手机号码
					$card_no,		//充值卡号码
					$card_pwd,		//充值卡密码
					$card_type,		//充值卡类型
					$money);
		print_r($data);
		//如果返回结果是result=0表示成功，返回result=1表示失败
		if(false == strstr($data,"result=0"))
		{
			die("-1|充值卡类型或者充值金额错误");
		}

		//写入充值表
		mysql_query("LOCK TABLES recharge WRITE");
		$result = mysql_query("INSERT INTO recharge (phone,card_no,card_pwd,card_type,money,flag,time) VALUES ('".$phone."','".$card_no."','".$card_pwd."','".$card_type."','".$money."','".$flag."','".$time."')");
		mysql_query("UNLOCK TABLES");

		if(false == $result)
		{
			mysql_close($con);
			die("-1|充值记录失败");
		}
		mysql_close($con);
		echo '0|';
	}
?>
